﻿using Cognex.VisionPro;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.ImageFile;

using System.Collections.Generic;
using System.Windows.Forms;

namespace 卡尺寻址
{
    public delegate List<double> PMADataDelegate(List<double> data);

    public partial class Main_Form : Form
    {
        public Main_Form MF;
        public PMADataDelegate pMADataDelegate;
        private readonly string imgPath = @"C:\Users\Diza9\Desktop\跑道.bmp";
        private int FindFlag;
        private bool IsSetCaliper = false;
        private CogCaliperTool mCogCaliperTool = new CogCaliperTool();
        private CogGeneralContour mCogGeneralContour = new CogGeneralContour();
        private CogRectangleAffine mCogRectangle = new CogRectangleAffine();
        private CogImageFileTool mImgTool = new CogImageFileTool();
        private ICogImage MyImg = null;
        private List<double> travelData = new List<double>();

        private float X, Y;

        public Main_Form()
        {
            InitializeComponent();
            MF = this;
        }

        public double FaultDown { get; set; } = 0;

        public double FaultUp { get; set; } = 0;

        public double FinalyResult { get; set; } = 0;

        private void button1_Click(object sender, System.EventArgs e)               //获取点位数据
        {
            定位 dw = new 定位();
        }

        private void button2_Click(object sender, System.EventArgs e)               //设置卡尺
        {
            if (FindFlag != 0)
            {
                return;
            }
            FindFlag = 1;

            if (IsSetCaliper)
            {
                return;
            }
            IsSetCaliper = !IsSetCaliper;
            //设置可控多边形
            mCogRectangle.Interactive = true;
            mCogRectangle.GraphicDOFEnableBase = CogGraphicDOFConstants.All;
            mCogRectangle.SetOriginLengthsRotationSkew(0, 0, 100, 20, 0, 0);
            //设置Caliper工具的测量区域
            mCogCaliperTool.Region = mCogRectangle;

            mCogCaliperTool.Region.XDirectionAdornment = CogRectangleAffineDirectionAdornmentConstants.SolidArrow;
            mCogCaliperTool.Region.YDirectionAdornment = CogRectangleAffineDirectionAdornmentConstants.Arrow;
            cogRecordDisplay1.InteractiveGraphics.Add(mCogCaliperTool.Region, "", false);
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            if (IsSetCaliper && FindFlag == 1)
            {
                IsSetCaliper = !IsSetCaliper;
                FindFlag = 2;
            }
        }

        private void button4_Click(object sender, System.EventArgs e)
        {
            定位 fm = new 定位(MyImg);
            fm.Show();
        }

        private void button5_Click(object sender, System.EventArgs e)
        {
            if (FindFlag != 2 || IsSetCaliper)
            {
                return;
            }
            FindFlag = 0;
        }

        private void button7_Click(object sender, System.EventArgs e)       //建议胶宽
        {
        }

        private void cbbPolar1_SelectionChangeCommitted(object sender, System.EventArgs e)
        {
            switch (cbbPolar1.SelectedText)
            {
                case "由白到黑":
                    mCogCaliperTool.RunParams.Edge0Polarity = CogCaliperPolarityConstants.LightToDark; break;
                case "由黑到白":
                    mCogCaliperTool.RunParams.Edge0Polarity = CogCaliperPolarityConstants.DarkToLight; break;
                case "任何极性":
                    mCogCaliperTool.RunParams.Edge0Polarity = CogCaliperPolarityConstants.DontCare; break;
            }
        }

        private void cbbPolar2_SelectionChangeCommitted(object sender, System.EventArgs e)
        {
            switch (cbbPolar1.SelectedText)
            {
                case "由白到黑":
                    mCogCaliperTool.RunParams.Edge1Polarity = CogCaliperPolarityConstants.LightToDark; break;
                case "由黑到白":
                    mCogCaliperTool.RunParams.Edge1Polarity = CogCaliperPolarityConstants.DarkToLight; break;
                case "任何极性":
                    mCogCaliperTool.RunParams.Edge1Polarity = CogCaliperPolarityConstants.DontCare; break;
            }
        }

        private void GetPMAData(List<double> data)
        {
        }

        private void Main_Form_Load(object sender, System.EventArgs e)
        {
            mImgTool.Operator.Open(imgPath, CogImageFileModeConstants.Read);
            mImgTool.Run();
            MyImg = mImgTool.OutputImage;

            cogRecordDisplay1.StaticGraphics.Clear();
            cogRecordDisplay1.InteractiveGraphics.Clear();
            cogRecordDisplay1.Image = null;
            cogRecordDisplay1.Image = MyImg;
            cogRecordDisplay1.Fit(true);

            FindFlag = 0;
            cbbPolar1.SelectedIndex = 0;
            cbbPolar2.SelectedIndex = 0;
            X = this.Width; Y = this.Height;
            Class1.GetTag(this);
        }

        private void Main_Form_Resize(object sender, System.EventArgs e)
        {
            float newX = X / this.Width;
            float newY = Y / this.Height;
            // Class1.SetControl(this, newX, newY);
        }

        private void numContrast_ValueChanged(object sender, System.EventArgs e)            //设置对比度阈值
        {
            mCogCaliperTool.RunParams.ContrastThreshold = (double)numContrast.Value;
        }

        private void numFault_ValueChanged(object sender, System.EventArgs e)       //设置容错率
        {
            if (numFilter.Value == 0)
            {
                numFilter.Value = 100;
                return;
            }
            if (FinalyResult == 0)
            {
                return;
            }
            FaultUp = FinalyResult * (1 + (double)numFilter.Value / 100);
            FaultDown = FinalyResult * (1 - (double)numFilter.Value / 100);
        }

        private void numFilter_ValueChanged(object sender, System.EventArgs e)      //设置过滤一半像素
        {
            mCogCaliperTool.RunParams.FilterHalfSizeInPixels = (int)numFilter.Value;
        }

        private void numGlueWidth_ValueChanged(object sender, System.EventArgs e)       //设置胶宽
        {
            FinalyResult = (double)numGlueWidth.Value;
        }
    }
}